<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .container {
            width: 1226px;
            min-height: 500px;
            margin: 0 auto;
            margin-top: 50px;
            display: flex;
            justify-content: space-between;
            align-items: stretch;
        }

        .container .magnifier {
            width: 400px;
            /* background-color: grey; */
            position: relative;
        }

        .container .item-info {
            flex: 1;
            margin-left: 20px;
            /* background-color: #f6f6f6; */

        }

        .container .magnifier .magnifier-preview {
            width: 400px;
            height: 400px;
            background-color: pink;
            position: relative;
        }

        .container .magnifier .magnifier-preview .smallImg {
            width: 100%;
        }

        .container .magnifier .magnifier-preview .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 0;
            top: 0;
            z-index: 99;
            display: none;
        }

        .container .magnifier .magnifier-list {
            height: 100px;
            margin-top: 10px;
            /* background-color: orange; */
        }

        .container .magnifier .magnifier-list .list {
            height: 100px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .magnifier .magnifier-list .list .list-item {
            width: 90px;
            height: 90px;
        }

        .container .magnifier .magnifier-list .list .list-item img {
            width: 100%;
        }


        .container .magnifier .magnifier-scale {
            width: 500px;
            height: 500px;
            background-color: rgba(125, 255, 255, 0.6);
            position: absolute;
            left: -webkit-calc(100% + 20px);
            top: 0;
            overflow: hidden;
            display: none;
        }

        .container .magnifier .magnifier-scale img {
            width: 1000px;
            height: 1000px;
            position: absolute;
            left: 0;
            top: 0;
            /* z-index: -1; */
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- position:relative -->
        <div class="magnifier">
            <div class="magnifier-preview">
                <img class="smallImg" src="../images/girlsmall1.jpg" alt="">
                <div class="shadow"></div>
            </div>
            <div class="magnifier-list">
                <!-- ul.list>li.list-item*4>img[src="../images/girlsmall$.jpg"] -->
                <ul class="list">
                    <li class="list-item"><img src="../images/girlsmall1.jpg" alt=""></li>
                    <li class="list-item"><img src="../images/girlsmall2.jpg" alt=""></li>
                    <li class="list-item"><img src="../images/girlsmall3.jpg" alt=""></li>
                    <li class="list-item"><img src="../images/girlsmall4.jpg" alt=""></li>
                </ul>
            </div>
            <!-- position:absolute -->
            <div class="magnifier-scale">
                <img src="../images/girlbig1.jpg" alt="" class="bigImg">
            </div>
        </div>
        <div class="item-info"></div>
    </div>
</body>
<script>

    /* 
        放大镜的实现步骤
        1. 呈比例关系放大
            shadow(200)           smallImg(400)      x(shadow)
        ————————————————      = ————————————————  = ———————————
         magnifierScale(500)      bigImg (1000)      X(bigImg)

        2. 左边移动的是shadow  右边移动的是bigImg

        3. 移动的距离也是等比例且反向的
    
    */

    /* 
        放大镜移入显示 移出隐藏的问题
        1. shadow的移动边界值收到影响 => maxLeft maxTop
        2. bigImg不再移动  =>  -scale*x  => shadow能移动,x y没问题 => scale有问题

        => clientWidth clientHeight 对于隐藏的元素(display:none),不生效 都是0 => 
        导致maxLeft maxTop scale计算出现偏差

        怎么解决?
        1. 既然隐藏时不生效,就等你显示的时候再计算
    
    */


    var magnifier = document.getElementsByClassName("magnifier")[0];
    var magnifierPreview = document.getElementsByClassName("magnifier-preview")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var magnifierScale = document.getElementsByClassName("magnifier-scale")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];

    var magnifierList = document.getElementsByClassName("magnifier-list")[0];
    var listItems = magnifierList.getElementsByClassName("list-item");

    var maxLeft = magnifierPreview.clientWidth - shadow.clientWidth;
    var maxTop = magnifierPreview.clientHeight - shadow.clientHeight;
    console.log(magnifierPreview.clientWidth, shadow.clientWidth);

    var scale = bigImg.clientWidth / smallImg.clientWidth;
    console.log(scale);

    var smallList = ["../images/girlsmall1.jpg", "../images/girlsmall2.jpg", "../images/girlsmall3.jpg", "../images/girlsmall4.jpg"];
    var bigList = ["../images/girlbig1.jpg", "../images/girlbig2.jpg", "../images/girlbig3.jpg", "../images/girlbig4.jpg"]

    Array.from(listItems).forEach(function (listItem, i) {
        listItem.onmouseenter = function () {
            smallImg.src = smallList[i];
            bigImg.src = bigList[i];
        }
    })

    magnifierPreview.onmouseenter = function () {
        shadow.style.display = "block";
        magnifierScale.style.display = "block";
    }

    magnifierPreview.onmousemove = function (e) {
        var e = e || window.event;

        var x = e.pageX - magnifier.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - magnifier.offsetTop - shadow.clientHeight / 2;

        if (x < 0) x = 0;
        if (x > maxLeft) x = maxLeft;
        if (y < 0) y = 0;
        if (y > maxTop) y = maxTop;

        shadow.style.left = x + "px";
        shadow.style.top = y + "px";
        bigImg.style.left = -scale * x + "px"
        bigImg.style.top = -scale * y + "px"


    }

    magnifierPreview.onmouseleave = function () {
        shadow.style.display = "none";
        magnifierScale.style.display = "none";
    }

</script>

</html>